<template>
    <div class="app-container">
        <!--工具栏-->
        <div class="head-container">
            <div v-if="crud.props.searchToggle">
                <!-- 搜索 -->
                <el-input v-model="query.blurry" clearable size="small" placeholder="订单号或者订单金额模糊搜索" style="width: 220px;"
                             class="filter-item" @keyup.enter.native="toQuery" />
                <el-select v-model="query.equipmentId" clearable class="filter-item" style="width: 150px" placeholder="请选择设备名称" @change="crud.toQuery" filterable>
                    <el-option
                    v-for="(item, index) in equipmentNames"
                    :key="item.equipmentId + index"
                    :label="item.equipmentName"
                    :value="item.equipmentId"
                    />
                </el-select>
                <el-select v-model="query.uid" clearable class="filter-item" style="width: 150px" placeholder="请选择用户名称" @change="crud.toQuery" filterable>
                    <el-option
                    v-for="(item, index) in users"
                    :key="item.username + index"
                    :label="item.username"
                    :value="item.uid"
                    />
                </el-select>
                <el-select v-model="query.agentId" clearable class="filter-item" style="width: 150px" placeholder="请选择代理商名称" @change="crud.toQuery" filterable>
                    <el-option
                    v-for="(item, index) in users"
                    :key="item.username + index"
                    :label="item.username"
                    :value="item.uid"
                    />
                </el-select>
                <rrOperation :crud="crud"/>
            </div>
            
            <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
            <crudOperation :permission="permission" style="display: none"/>
            <!--表格渲染-->
            <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                      @selection-change="crud.selectionChangeHandler">
                <el-table-column type="selection" width="55"/>
                <el-table-column v-if="columns.visible('orderId')" prop="orderId" min-width="130px" label="订单号"/>
                <el-table-column v-if="columns.visible('equipmentName')" prop="equipmentName" label="设备名称"/>
                <el-table-column v-if="columns.visible('userName')" prop="userName" min-width="90px" label="用户名"/>
                <el-table-column v-if="columns.visible('agentName')" prop="agentName" min-width="90px" label="代理商名称"/>
                <el-table-column v-if="columns.visible('amount')" prop="amount" label="订单金额"/>
                <el-table-column v-if="columns.visible('userAmount')" prop="userAmount" label="用户返还金额" min-width="60px"/>
                <el-table-column v-if="columns.visible('agentAmount')" prop="agentAmount" label="代理佣金金额" min-width="60px"/>
                <el-table-column v-if="columns.visible('companyAmount')" prop="companyAmount" label="公司收入金额" min-width="60px"/>
                <el-table-column v-if="columns.visible('paid')" label="是否已结算" align="center" prop="paid">
                    <template slot-scope="scope">
                    <div>
                        <el-tag v-if="scope.row.paid === 1" :type="''">是</el-tag>
                        <el-tag v-else :type=" 'info' ">否</el-tag>
                    </div>
                    </template>
                </el-table-column>
                <el-table-column v-if="columns.visible('paidTime')" prop="paidTime" label="结算时间">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.paidTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="创建时间">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
            </el-table>
            <!--分页组件-->
            <pagination/>
        </div>
    </div>
</template>

<script>
    import CRUD, {crud, form, header, presenter} from '@crud/crud'
    import rrOperation from '@crud/RR.operation'
    import crudOperation from '@crud/CRUD.operation'
    import udOperation from '@crud/UD.operation'
    import pagination from '@crud/Pagination'
    import MaterialList from "@/components/material";
    import { getAllMemberUser } from '@/api/memberUser'
    import { getEquipmentNameList } from '@/api/equipmentOrder'

    // crud交由presenter持有
    const defaultCrud = CRUD({
        title: 'EquipmentOrder',
        url: 'api/equipmentOrder',
        sort: 'id,desc'
    })
    const defaultForm = {id: null, orderId: null, userName: null, equipmentName: null, agentName: null, amount: null, paid: null, userAmount: null, agentAmount: null, companyAmount: null, paidTime: null, createTime: null, updateTime: null, isDel: null}
    export default {
        name: 'EquipmentOrder',
        components: {pagination, crudOperation, rrOperation, udOperation , MaterialList},
        mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
        data() {
            return {
                users: [],
                equipmentNames: [],
                permission: {
                    add: ['admin', 'equipmentOrder:add'],
                    edit: ['admin', 'equipmentOrder:edit'],
                    del: ['admin', 'equipmentOrder:del']
                },
                rules: {
                },
            }
        },
        watch: {
        },
        mounted:function(){ //在页面加载参数结束后执行某个函数
            this.$nextTick(function(){
                //调用需要执行的方法
                this.getMemberUser()
                this.getEquipmentNames()
            })      
        },
        methods: {
            // 获取数据前设置好接口地址
            [CRUD.HOOK.beforeRefresh]() {
                const query = this.query
                if (query.type && query.value) {
                    this.crud.params[query.type] = query.value
                } else {
                    delete this.crud.params.uid
                }
                return true
            }, // 新增与编辑前做的操作
            [CRUD.HOOK.afterToCU](crud, form) {

            },
            getMemberUser() {
                getAllMemberUser().then(res => {
                    this.users = res
                }).catch(() => { })
            },
            getEquipmentNames() {
                getEquipmentNameList().then(res => {
                    this.equipmentNames = res
                }).catch(() => { })
            },
            
        }
    }


</script>

<style scoped>
    .table-img {
        display: inline-block;
        text-align: center;
        background: #ccc;
        color: #fff;
        white-space: nowrap;
        position: relative;
        overflow: hidden;
        vertical-align: middle;
        width: 32px;
        height: 32px;
        line-height: 32px;
    }

</style>
